package com.wq.data.order.repository;


import com.wq.api.domain.entity.order.Order;
import com.wq.api.domain.entity.order.OrderLine;
import com.wq.api.repository.order.OrderRepository;
import com.wq.data.order.dao.OrderLineMapper;
import com.wq.data.order.dao.OrderMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.stereotype.Repository;

/**
 * Created by changming.xie on 4/1/16.
 */
@Repository
@DubboService
public class OrderRepositoryImpl implements OrderRepository {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderLineMapper orderLineMapper;

    @Override
    public void createOrder(Order order) {
        orderMapper.insert(order);

        for (OrderLine orderLine : order.getOrderLines()) {
            orderLineMapper.insert(orderLine);
        }
    }

    @Override
    public void updateOrder(Order order) {
        order.updateVersion();
        int effectCount = orderMapper.update(order);

        if (effectCount < 1) {
            throw new OptimisticLockingFailureException("update order failed");
        }
    }

    @Override
    public Order findByMerchantOrderNo(String merchantOrderNo) {
        return orderMapper.findByMerchantOrderNo(merchantOrderNo);
    }
}
